package com.zzuli.yxy.controller;

import com.zzuli.yxy.entity.Employee;
import com.zzuli.yxy.enumeration.AppHttpCodeEnum;
import com.zzuli.yxy.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.zzuli.yxy.service.EmployeeService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


/**
 * @auther pony
 * @create 2023-03-05 14:47
 */
@RestController
@RequestMapping("employee")
@CrossOrigin(origins = "*")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 验证码接口-以流的形式写回验证码图片
     * @param request
     * @param response
     */
    @GetMapping("getVerifyCode")
    public void getCaptcha(HttpServletRequest request, HttpServletResponse response){
        employeeService.getVerifyCode(request, response);
    }

    /**
     * 登录
     * @param employee
     * @param verifyCode
     * @param request
     * @return
     */
    @PostMapping("login/{verifyCode}")
    public R login(@RequestBody Employee employee, @PathVariable("verifyCode") String verifyCode, HttpServletRequest request){
        return employeeService.login(employee, verifyCode, request);
    }

    /**
     * 登出接口
     * @return
     */
    @GetMapping("logout")
//    @PreAuthorize("hasAuthority('system:test:list')")
    public R logout(){
        return employeeService.logout();
    }

    /**
     * 上传头像并更改
     * @param file
     * @param request
     * @return
     */
    @PostMapping("uploadImage")
    public R uploadImage(MultipartFile file, HttpServletRequest request){
        return employeeService.uploadImage(file, request);
    }

    /**
     * 解析token，获取员工信息
     * @param request
     * @return
     */
    @GetMapping("/info")
    public R getEmployeeInfoByToken(HttpServletRequest request){
        return employeeService.getEmployeeInfoByToken(request);
    }

    /**
     * 获取菜单列表信息
     * @param request
     * @return
     */
    @GetMapping("/menu")
    public R getMenuList(HttpServletRequest request){
        return employeeService.getMenuList(request);
    }

    /**
     * 获取员工管理分页信息
     * @param pageCurrent 当前页
     * @param pageSize 每页条数
     * @param name 关键字 非必传
     * @return
     */
    @GetMapping({"/page/{pageCurrent}/{pageSize}",
            "/page/{pageCurrent}/{pageSize}/{name}"})
    public R getPage(@PathVariable("pageCurrent") Integer pageCurrent,
                     @PathVariable("pageSize") Integer pageSize,
                     @PathVariable(name = "name", required = false) String name){
        return employeeService.getPage(pageCurrent, pageSize, name);
    }

    /**
     * 更改员工当前状态
     * @param status 状态码 0：正常 1：异常
     * @return
     */
    @GetMapping("/status/{id}/{status}")
    public void setStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status){
        employeeService.setStatus(id, status);
    }

    /**
     * 解析token，获取员工角色名称
     * @param request
     * @return
     */
    @GetMapping("/getRoleName")
    public R getRoleNameByEmployeeId(HttpServletRequest request){
        return employeeService.getRoleNameByEmployeeId(request);
    }

    /**
     * 根据员工id，查询单个员工信息
     * @param id
     * @return
     */
    @GetMapping("/selectOne/{id}")
    public R selectOneById(@PathVariable("id") Integer id){
        return employeeService.selectOneById(id);
    }

    /**
     * 修改员工信息
     * @param employee
     * @return
     */
    @PutMapping("/update")
    public R updateEmployee(@RequestBody Employee employee){
        //非空判断
        if ("".equals(employee.getUsername())||employee.getUsername()==null){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return employeeService.updateEmployee(employee);
    }

    /**
     * 修改用户密码
     * @return
     */
    @PutMapping("/update/{originalPassword}/{password}")
    public R updateEmployeePasswordById(@PathVariable("originalPassword") String originalPassword, @PathVariable("password") String password, HttpServletRequest request){
        return employeeService.updateEmployeePasswordById(originalPassword, password, request);
    }

    /**
     * 添加员工
     * @param employee
     * @return
     */
    @PostMapping("/add")
    public R addEmployee(@RequestBody Employee employee) {
        //1、非空判断
        if ("".equals(employee.getUsername())){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        if ("".equals(employee.getPassword())){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return employeeService.addEmployee(employee);
    }

    /**
     * 根据员工 id，删除员工
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public R deleteEmployeeById(@PathVariable("id") Integer id) {
        return employeeService.deleteEmployeeById(id);
    }

    /**
     * 批量删除
     * @param ids 员工 id数组
     * @return
     */
    @DeleteMapping("deleteBatch")
    public R deleteEmployeeBatch(@RequestBody List<Integer> ids){
        //非空判断
        if(ids == null || ids.size() ==0 ){
            return R.error(AppHttpCodeEnum.PARAMETER_LACK);
        }
        return employeeService.deleteEmployeeBatch(ids);
    }

    /**
     * 员工信息 -- 导出 Excel
     * @param response
     * @return
     */
    @GetMapping("outExcel")
    public void outExcel(HttpServletResponse response){
        employeeService.outExcel(response);
    }

    /**
     * 存储基本信息 -- 导入 Excel
     *
     */
//    @PostMapping("inExcel")
//    public R inExcel(MultipartFile file)  {
//        employeeService.inExcel(file);
//        return R.success().message("上传成功!");
//    }

    /**
     * 为 员工添加角色
     * @param id 待添加的员工id
     * @param roleIds 添加的角色id数组
     * @return
     */
    @PostMapping("addRoleForEmployee/{id}")
    public R addRoleForEmployee(@PathVariable("id") Integer id, @RequestBody List<Integer> roleIds){
//        System.out.println("员工id："+id);
//        for (Integer roleId: roleIds) {
//            System.out.println("角色id："+roleIds);
//        }
        return employeeService.addRoleForEmployee(id, roleIds);
    }


    @GetMapping("test")
    @PreAuthorize("hasAuthority('system:test:list')")
    public R test(){
        return R.success().message("测试成功");
    }
}
